function Utils() {
};

Utils.PAGE_TYPE_GENERAL = 0;
Utils.PAGE_TYPE_DUPLICATE = 1;
Utils.PAGE_TYPE_URL = 2;

Utils.getPageTypes = function() {
  return [ { id: 0, name: '{{t escape='javascript'}}Normal{{/t}}' },
           { id: 1, name: '{{t escape='javascript'}}Redirect to Page{{/t}}' },
           { id: 2, name: '{{t escape='javascript'}}Redirect to URL{{/t}}' }
         ];
};

Utils.ATTRIBUTE_TYPE_SINGLE_OPTION = 0;
Utils.ATTRIBUTE_TYPE_MULTIPLE_OPTIONS = 1;
Utils.ATTRIBUTE_TYPE_CUSTOMER_INPUT = 2;

Utils.getAttributeTypes = function() {
  return [ { id: 0, name: '{{t escape='javascript'}}Single Choice{{/t}}' },
           { id: 1, name: '{{t escape='javascript'}}Multiple Choices{{/t}}' },
           { id: 2, name: '{{t escape='javascript'}}Customer Input{{/t}}' },
         ];
};

Utils.getAttributeType = function(id) {
  var types = Utils.getAttributeTypes();
  for (var idx in types) {
    var obj = types[idx];
    if (obj.id == id) {
      return obj.name;
    }
  }
  return '';
};

Utils.createMenuSel = function(select, node, nums, unwanted_nodes, selected_id) {
  var existed = false;
  for (var i = 0, il = unwanted_nodes.length; i < il; i++) {
    var tmp = unwanted_nodes[i];
    if (tmp.id == node.id) {
      existed = true;
    }
  }
  
  var prefix = '(';
  for (var idx in nums) {
    prefix += nums[idx];
    if (idx != nums.length - 1) {
      prefix += '-';
    }
  }
  prefix += ')';
  prefix = (prefix == '()') ? '' : prefix;
  
  var span = document.createElement('span');
  span.style.marginLeft = (nums.length) * 10 + 'px';
  span.style.marginRight = '5px';
  span.appendChild(document.createTextNode(prefix));
  
  var op = new Option();
  op.appendChild(span);
  if (nums.length != 0) {
    op.appendChild(document.createTextNode(node.name));
  } else {
    op.appendChild(document.createTextNode('{{t escape='javascript'}}Select...{{/t}}'));
  }
  op._n = node;
  op.disabled = existed;
  select.options[select.options.length] = op;
  
  if (selected_id == node.id) {
    select.selectedIndex = select.options.length - 1;
  }
  
  for (var i = 0, il = node.c.length; i < il; i++) {
    var tmp = node.c[i];
    nums.push(i + 1);
    this.createMenuSel(select, tmp, nums, unwanted_nodes, selected_id);
    nums.pop();
  }
};

Utils.findMenuSpan = function(node, id, nums) {
  if (node.id == id) {
    var prefix = '(';
    for (var idx in nums) {
      prefix += nums[idx];
      if (idx != nums.length - 1) {
        prefix += '-';
      }
    }
    prefix += ')';
    prefix = (prefix == '()') ? '' : prefix;
    
    var elm = document.createElement('span');
    elm.style.marginRight = '5px';
    elm.appendChild(document.createTextNode(prefix));
    
    var span = document.createElement('span');
    span.appendChild(elm);
    span.appendChild(document.createTextNode(MiscUtils.getShortName(node.name, 20)));
    return span;
  }
  
  for (var i = 0, il = node.c.length; i < il; i++) {
    nums.push(i + 1);
    var tmp = node.c[i];
    var span = this.findMenuSpan(tmp, id, nums);
    if (span) {
      return span;
    }
    nums.pop();
  }
  
  return null;
};

Utils.findMenu = function(node, id) {
  if (node.id == id) {
    return node;
  }
  
  for (var i = 0, il = node.c.length; i < il; i++) {
    var tmp = node.c[i];
    var result = this.findMenu(tmp, id);
    if (result) {
      return result;
    }
  }
  
  return null;
};

Utils.imgBtn = function(text, imgPath) {
  var table, tr, td, span, elm;
  span = document.createElement('div');
  span.className = 'image_button';
  table = document.createElement('table');
  tr = table.insertRow(-1);
  td = tr.insertCell(-1);
  elm = document.createElement('img');
  elm.src = imgPath;
  td.appendChild(elm);
  td = tr.insertCell(-1);
  td.appendChild(document.createTextNode(text));
  span.appendChild(table);
  return span;
};

Utils.imgDisp = function(file_obj, del_file_func, thumb_width, thumb_height, container) {
  var table, tr, td, elm;

  container.style.height = thumb_height + 'px';
  
  if (file_obj.small != '') {
    table = document.createElement('table');
    container.appendChild(table);
  
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.verticalAlign = 'top';
    td.style.width = thumb_width + 'px';
    td.style.height = thumb_height + 'px';
    if (file_obj._large != '') {
      new MyPopImage(td, file_obj._large, file_obj._small);
    }
    td = tr.insertCell(-1);
    if (del_file_func) {
      td.style.verticalAlign = 'top';
      td.style.width = '20px';
      elm = document.createElement('img');
      elm.style.cursor = 'pointer';
      elm.src = 'image/delete.png';
      elm.onclick = function() { del_file_func(file_obj); };
      td.appendChild(elm);
    }
  }
};
